package org.jeecg.modules.xs.service.impl;

import org.jeecg.modules.xs.entity.ProductInfo;
import org.jeecg.modules.xs.entity.ProductClerk;
import org.jeecg.modules.xs.entity.ProducImg;
import org.jeecg.modules.xs.mapper.ProductClerkMapper;
import org.jeecg.modules.xs.mapper.ProducImgMapper;
import org.jeecg.modules.xs.mapper.ProductInfoMapper;
import org.jeecg.modules.xs.service.IProductInfoService;
import org.springframework.stereotype.Service;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import java.io.Serializable;
import java.util.List;
import java.util.Collection;

/**
 * @Description: 商品
 * @Author: sss
 * @Date:   2022-02-15
 * @Version: V1.0
 */
@Service
public class ProductInfoServiceImpl extends ServiceImpl<ProductInfoMapper, ProductInfo> implements IProductInfoService {

	@Autowired
	private ProductInfoMapper productInfoMapper;
	@Autowired
	private ProductClerkMapper productClerkMapper;
	@Autowired
	private ProducImgMapper producImgMapper;
	
	@Override
	@Transactional
	public void saveMain(ProductInfo productInfo, List<ProductClerk> productClerkList,List<ProducImg> producImgList) {
		productInfoMapper.insert(productInfo);
		if(productClerkList!=null && productClerkList.size()>0) {
			for(ProductClerk entity:productClerkList) {
				//外键设置
				entity.setProductId(productInfo.getId());
				productClerkMapper.insert(entity);
			}
		}
		if(producImgList!=null && producImgList.size()>0) {
			for(ProducImg entity:producImgList) {
				//外键设置
				entity.setProductId(productInfo.getId());
				producImgMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void updateMain(ProductInfo productInfo,List<ProductClerk> productClerkList,List<ProducImg> producImgList) {
		productInfoMapper.updateById(productInfo);
		
		//1.先删除子表数据
		productClerkMapper.deleteByMainId(productInfo.getId());
		producImgMapper.deleteByMainId(productInfo.getId());
		
		//2.子表数据重新插入
		if(productClerkList!=null && productClerkList.size()>0) {
			for(ProductClerk entity:productClerkList) {
				//外键设置
				entity.setProductId(productInfo.getId());
				productClerkMapper.insert(entity);
			}
		}
		if(producImgList!=null && producImgList.size()>0) {
			for(ProducImg entity:producImgList) {
				//外键设置
				entity.setProductId(productInfo.getId());
				producImgMapper.insert(entity);
			}
		}
	}

	@Override
	@Transactional
	public void delMain(String id) {
		productClerkMapper.deleteByMainId(id);
		producImgMapper.deleteByMainId(id);
		productInfoMapper.deleteById(id);
	}

	@Override
	@Transactional
	public void delBatchMain(Collection<? extends Serializable> idList) {
		for(Serializable id:idList) {
			productClerkMapper.deleteByMainId(id.toString());
			producImgMapper.deleteByMainId(id.toString());
			productInfoMapper.deleteById(id);
		}
	}
	
}
